home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13858 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.salford.ac.uk!aber!not-for-mail
  2. From: pcg@aber.ac.uk (Piercarlo Grandi)
  3. Newsgroups: comp.lang.c++,comp.object,comp.object.logic
  4. Subject: Re: polymorphism
  5. Date: 27 Mar 1996 19:49:43 +0000
  6. Organization: Prifysgol Cymru, Aberystwyth
  7. Sender: pcg@osfb.aber.ac.uk
  8. Message-ID: <vwjvijql3jc.fsf@osfb.aber.ac.uk>
  9. References: <31513CBB.41C6@mi.leeds.ac.uk> <3151DCF0.3EAA@staff.ichange.com>
  10. Reply-To: pcg@aber.ac.uk (Piercarlo Grandi)
  11. NNTP-Posting-Host: osfb.aber.ac.uk
  12. In-reply-to: Jesse Liberty's message of Thu, 21 Mar 1996 17:49:20 -0500
  13. X-Newsreader: Gnus v5.0.15
  14.  
  15. >>> On Thu, 21 Mar 1996 17:49:20 -0500, Jesse Liberty
  16. >>> <jl@staff.ichange.com> said:
  17.  
  18. jl> Aniko Simon wrote:
  19.  
  20. Aniko> Can anyone help: what does the term "polymorphism" mean in OO
  21. Aniko> programming ?
  22.  
  23. jl> Polymorphism refers to the ability to take more than one form.
  24.  
  25. In Greek mythology and in nethack, oh yes.
  26.  
  27. But in OO programming it means exactly the opposite: the ability to
  28. maintain exactly the same form, as you actually describe a bit
  29. imprecisely but correctly:
  30.  
  31. jl> The idea is that the user of an object can deal with an abstract
  32. jl> interface and the individual implementation "does the right thing."
  33.  
  34. This is imprecise because it only describes one of the two very
  35. different things that are usually called polymorphism (ad hoc): the
  36. ability of the *same* interface to be applied to values of different
  37. types, which is called overloading. For example:
  38.  
  39.     2 + 3 == 5
  40.  
  41.     "a" + "b" == "ab"
  42.  
  43. The *same* symbols, '+' and '==' can without change be applied both to
  44. integers and strings, because as interfaces they are associated with
  45. very different implementations.
  46.  
  47. There is another concept that is also called (proper) polymorphism, and
  48. it is genericity, that is the ability of the *same* implementation to
  49. apply to values of different types. For example:
  50.  
  51.     (lambda (a b) (if (> a b) a b))
  52.  
  53. is generic wrt any type for which '>' is defined, because the *same*
  54. implementation can be used for any of them.
  55.  
  56. jl> For example, you might tell a window to draw and have it draw
  57. jl> properly whether it is a frame, a subwindow, a button, or a check
  58. jl> box.  The different "forms" of the window all do the right thing in
  59. jl> their own context.
  60.  
  61. This is completely wrong: windows don't to anything, what happens is
  62. that the same interface resolves to different implementations depending
  63. on the type of window, nothing changes form at all, despite the
  64. misnomer.
  65.  
  66. It is unfortunate that in OO ``polymorphism'' indicates exactly the
  67. opposite of what it means in literature, but it is very important to
  68. realize that, because the ability to apply the same interface or the
  69. same implementation, _without_ change of form, to values of different
  70. types allows higher reuse of code, which is supposedly a good idea.
  71.